//
//  SwapViewController.swift
//  AwesomeUUIKitModule_Example
//
//  Created by Miss_Special on 2018/4/17.
//  Copyright © 2018 CocoaPods. All rights reserved.
//

import UIKit

import AwesomeUBaseModule
import AwesomeUUIKitModule

import SnapKit

class SwapViewController: UIViewController {
    
    let au_kSwapTitleViewHeight: CGFloat = 40 * au_kScreenScale
    
    /// 标题
    fileprivate lazy var titleView: AUSwapTitleView = {
        let titles = ["推荐", "视频", "图片", "笑话", "排行"]
        var maxY: CGFloat = 0
        if #available(iOS 11.0, *) {
            maxY += (navigationController?.navigationBar.frame.maxY ?? 0)
        }
        let titleView = AUSwapTitleView(frame: CGRect(x: 0, y: maxY, width: au_kScreenWidth, height: au_kSwapTitleViewHeight), titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    /// 内容
    fileprivate lazy var contentView: AUSwapContentView = { [weak self] in
        let vc1 = UIViewController()
        vc1.view.backgroundColor = UIColor.green
        let vc2 = UIViewController()
        vc2.view.backgroundColor = UIColor.blue
        let vc3 = UIViewController()
        vc3.view.backgroundColor = UIColor.yellow
        let vc4 = UIViewController()
        vc4.view.backgroundColor = UIColor.cyan
        let vc5 = UIViewController()
        vc5.view.backgroundColor = UIColor.lightGray
        
        let childs = [vc1, vc2, vc3, vc4, vc5]
        let frame = CGRect(x: 0, y: self!.titleView.frame.maxY, width: au_kScreenWidth, height: au_kScreenHeight - self!.titleView.frame.maxY)
        let contentView = AUSwapContentView(frame: frame, childViewControllers: childs, parrentViewController: self)
        contentView.delegate = self
        return contentView
        }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "可滑动的视图容器"
        
        setupUI()
    }

}

extension SwapViewController {
    
    fileprivate func setupUI() {
        
        view.backgroundColor = UIColor.white
        
        view.addSubview(titleView)
        
        view.addSubview(contentView)
    }
}

extension SwapViewController: AUSwapTitleViewDelegate {
    func swapTitleView(_ titleView: AUSwapTitleView, clickedIndex index: Int) {
        contentView.setCurrentPage(index)
    }
}

extension SwapViewController: AUSwapContentViewDelegate {
    func swapContentView(_ swapContentView: AUSwapContentView, progress: CGFloat, sourceIndex: Int, targetIdex: Int) {
        titleView.setProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIdex)
    }
}
